package com.zhilingshenghuo.app.zhly.view.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import com.zhilingshenghuo.app.zhly.R
import kotlinx.android.synthetic.main.header_bar_layout.view.*

class HeadBar @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
    private var isShowReturnIv: Boolean = true              //默认显示左边返回键
    private var isShowHeardLine: Boolean = true             //默认显示下划线
    private var isShowRightImage: Boolean = false           //默认不显示右边图片
    private var isShowRightText: Boolean = false           //默认不显示右边文字
    private var isReturn: Boolean = true                     //是否返回
    private var isShowBackground: Boolean = true            //默认显示背景
    private var isMiddleWhite: Boolean = false              //默认中间文字为黑色  true为白色
    private var titleLeftText: String? = null               //左边文字
    private var titleLeftTwoText: String? = null            //左边第二个文字
    private var middleText: String? = null                  //title文字
    private var headRightText: String? = null               //右边文字
    private var leftImage: Int = R.drawable.icon_back        //左边图片
    private var leftImageColor: Int = 0xC50032             //右边图片颜色
    private var showLeftImageColor: Boolean = false        //显示右边图片颜色
    private var rightImage: Int = R.drawable.icon_back      //右边图片
    private var rightTextColor: Int = 0xC50032             //右边文字颜色
    private var rightImageColor: Int = 0xC50032             //右边图片颜色
    private var showRightImageColor: Boolean = false        //显示右边图片颜色
    private var onReturn: (() -> Unit)? = null

    private var onRightClick: (() -> Unit)? = null
    private var onRightTextClick: (() -> Unit)? = null
    private var b: Boolean = false
    private var onRightTextGone: ((Boolean) -> Unit)? = null

    fun onReturn(onReturn: () -> Unit): HeadBar {
        this.onReturn = onReturn
        return this
    }


    fun onRightClick(onReturn: () -> Unit): HeadBar {
        this.onRightClick = onReturn
        return this
    }

    fun onRightTextClick(onReturn: () -> Unit): HeadBar {
        this.onRightTextClick = onReturn
        return this
    }

    init {
        val typeArray = context.obtainStyledAttributes(attrs, R.styleable.HeadBar)
        isShowReturnIv = typeArray.getBoolean(R.styleable.HeadBar_isShowReturnIv, isShowReturnIv)
        isShowHeardLine = typeArray.getBoolean(R.styleable.HeadBar_isShowHeardLine, isShowHeardLine)
        isShowRightImage =
            typeArray.getBoolean(R.styleable.HeadBar_isShowRightImage, isShowRightImage)
        isShowRightText = typeArray.getBoolean(R.styleable.HeadBar_isShowRightText, isShowRightText)
        isShowBackground =
            typeArray.getBoolean(R.styleable.HeadBar_isShowBackground, isShowBackground)
        isMiddleWhite = typeArray.getBoolean(R.styleable.HeadBar_isMiddleWhite, isMiddleWhite)
        middleText = typeArray.getString(R.styleable.HeadBar_middleText)
        titleLeftText = typeArray.getString(R.styleable.HeadBar_titleLeftText)
        titleLeftTwoText = typeArray.getString(R.styleable.HeadBar_titleLeftTwoText)
        headRightText = typeArray.getString(R.styleable.HeadBar_headRightText)
        leftImage = typeArray.getResourceId(R.styleable.HeadBar_leftImage, leftImage)
        rightImage = typeArray.getResourceId(R.styleable.HeadBar_rightImage, rightImage)
        rightImageColor = typeArray.getColor(R.styleable.HeadBar_rightImageColor, rightImageColor)
        leftImageColor = typeArray.getColor(R.styleable.HeadBar_leftImageColor, leftImageColor)
        rightTextColor = typeArray.getColor(R.styleable.HeadBar_rightTextColor, rightTextColor)
        showRightImageColor =
            typeArray.getBoolean(R.styleable.HeadBar_showRightImageColor, showRightImageColor)
        showLeftImageColor =
            typeArray.getBoolean(R.styleable.HeadBar_showLeftImageColor, showLeftImageColor)
        typeArray.recycle()
        initView()
    }

    private fun initView() {
        View.inflate(context, R.layout.header_bar_layout, this)
        if (isShowReturnIv) heard_return_iv.visibility =
            View.VISIBLE else heard_return_iv.visibility = View.GONE
        if (isShowHeardLine) heard_line.visibility = View.VISIBLE else heard_line.visibility =
            View.GONE
        if (isShowRightImage) heard_right_iv.visibility =
            View.VISIBLE else heard_right_iv.visibility = View.GONE
        if (isShowRightText) right_text.visibility = View.VISIBLE else right_text.visibility =
            View.GONE
        if (isShowBackground) heard_bg.visibility = View.VISIBLE else heard_bg.visibility =
            View.GONE
        if (isMiddleWhite) heard_title_tv.setTextColor(
            ContextCompat.getColor(
                context,
                R.color.app_white_typeface
            )
        ) else heard_title_tv.setTextColor(
            ContextCompat.getColor(context, R.color.app_white_typeface)
        )
        if (!titleLeftText.isNullOrEmpty()) {
            heard_left_rl.visibility = View.VISIBLE
        } else {
            heard_left_rl.visibility = View.GONE
        }
        if (showRightImageColor) {
            heard_right_iv.setColorFilter(rightImageColor)
        }
        if (showLeftImageColor) {
            heard_return_iv.setColorFilter(leftImageColor)
        }
        right_text.setTextColor(rightTextColor)
        middleText?.let { heard_title_tv.text = it }
        titleLeftText?.let { heard_left_tv.text = it }
        titleLeftTwoText?.let { heard_left_two_tv.text = it }
        heard_right_iv.setImageResource(rightImage)
        heard_return_iv.setImageResource(leftImage)
        heard_return_iv.setOnClickListener { onReturn?.invoke() }
        heard_right_iv.setOnClickListener { onRightClick?.invoke() }
        right_text.setOnClickListener { onRightTextClick?.invoke() }
        if (headRightText.isNullOrEmpty()) {
            right_text.visibility = View.GONE
        } else {
            right_text.text = headRightText
            right_text.visibility = View.VISIBLE
        }
    }

    //右边文字是否显示
    fun isShowRightText(b: Boolean) {
        if (b) {
            right_text.visibility = View.VISIBLE
        } else {
            right_text.visibility = View.GONE
        }
    }

    fun setLeftText(leftText: String?) {
        leftText?.let { heard_left_tv.text = leftText }
    }

    fun setLeftTwoText(leftTwoText: String?) {
        leftTwoText?.let { heard_left_two_tv.text = leftTwoText }
    }

    fun setTitle(title: String?) {
        title?.let { heard_title_tv.text = title }
    }

    fun getTitle(): String {
        return heard_title_tv.text.toString()
    }

    fun setRightText(rightText: String?) {
        rightText?.let {
            right_text.visibility = View.VISIBLE
            right_text.text = rightText
        }
    }

    fun getRightText(): String {
        return right_text.text.toString()
    }
}